﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Domain
{
    public class HuffmanTreeNode
    {
        public char Symbol { get; set; }
        public int Frequency { get; set; }
        public HuffmanTreeNode Right { get; set; }
        public HuffmanTreeNode Left { get; set; }

        public bool IsLeaf()
        {
            return (Right == null && Left == null);
        }

        public List<bool> GetSymbolCode(char symbol, List<bool> data)
        {
            if (IsLeaf())
            {
                if (symbol.Equals(this.Symbol))
                {
                    return data;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                List<bool> LeftCode = null;
                List<bool> RightCode = null;

                if (Left != null)
                {
                    List<bool> leftPath = new List<bool>();
                    leftPath.AddRange(data);
                    leftPath.Add(false);

                    LeftCode = Left.GetSymbolCode(symbol, leftPath);
                }

                if (Right != null)
                {
                    List<bool> rightPath = new List<bool>();
                    rightPath.AddRange(data);
                    rightPath.Add(true);

                    RightCode = Right.GetSymbolCode(symbol, rightPath);
                }

                if (LeftCode != null)
                {
                    return LeftCode;
                }
                else
                {
                    return RightCode;
                }
            }
        }
    }
}
